<?php
/** 
 * @copyright Copyright (C) 2010 redCOMPONENT.com. All rights reserved. 
 * @license GNU/GPL, see license.txt or http://www.gnu.org/copyleft/gpl.html
 * Developed by email@recomponent.com - redCOMPONENT.com 
 *
 * redSHOP can be downloaded from www.redcomponent.com
 * redSHOP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with redSHOP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');
//require_once( JPATH_COMPONENT.DS.'helpers'.DS.'extra_field.php' );
class attribute_setModelattribute_set extends JModel
{
	var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_table_prefix = null;
	var $_context = null;
	
	function __construct()
	{
		parent::__construct();

		global $mainframe;
	
		$this->_context='attribute_set_id';
	  	$this->_table_prefix = '#__redshop_';		
	  		
		$limit	= $mainframe->getUserStateFromRequest( $this->_context.'limit', 'limit', $mainframe->getCfg('list_limit'), 0);
		$limitstart = $mainframe->getUserStateFromRequest( $this->_context.'limitstart', 'limitstart', 0 );
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		
		$search_field = $mainframe->getUserStateFromRequest( $this->_context.'search_field',  'search_field', '' );
		$keyword = $mainframe->getUserStateFromRequest( $this->_context.'keyword',  'keyword', '' );
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		$this->setState('search_field', $search_field);
		$this->setState('keyword', $keyword);
	}
	function getData()
	{		
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
	
	function getTotal()
	{
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}
	
	function getPagination()
	{
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}
  	
	function _buildQuery()
	{
		$orderby	= $this->_buildContentOrderBy();
		$keyword = $this->getState('keyword');
		$search_field = $this->getState('search_field');
		
		$where = '';
		if(trim($keyword)!='') 
		{
			$where .= "AND attribute_set_name LIKE '%$keyword%' ";
		}
		$query = 'SELECT distinct(a.attribute_set_id),a.* FROM '.$this->_table_prefix.'attribute_set AS a '
	   			.'WHERE 1=1 '
	   			.$where
	   			.$orderby;
		return $query;
	}
	
	function _buildContentOrderBy()
	{
		global $mainframe;
		
		$filter_order     = $mainframe->getUserStateFromRequest( $this->_context.'filter_order',      'filter_order', 	  'attribute_set_id' );		
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $this->_context.'filter_order_Dir',  'filter_order_Dir', '' );
				
		if($filter_order != '' && $filter_order_Dir != '' )
			 $orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
		else 
			$orderby 	= ' ORDER BY attribute_set_id ASC';			
		 		
		return $orderby;
	}
}?>